Ext.define("component.file.attachment",{
	extend : 'Ext.panel.Panel',
	config : {
		objectId: '',
		imageType: '',
		contextPath:contextPath,
		showBtn: '',
		showDelBtn: ''
	},
	getFileArrs:function(){
		 var fileArrUrls = Ext.getCmp("fileUrlArr").getValue();
		 var fileArrNames = Ext.getCmp("fileArrNames").getValue();
		 if(fileArrUrls!="" && fileArrNames!=""){
			 return fileArrUrls+";"+fileArrNames;
		 }
		 return ""; 
	},
	deleteBtnHidden:function(){
		var panel=Ext.getCmp("show_img_panel");
		var items = Ext.ComponentQuery.query('button',panel);
		Ext.Array.forEach(items,function(item){ //单纯的遍历数组
			item.setVisible(false);
		});
	},
	initComponent : function() {
		var me = this;
		var objectId = this.objectId;
		var imageType = this.imageType;
		var uploadBtnShow = false;
		var showBtn = this.showBtn;
		var contextPath=this.contextPath;
		var showDelBtn = this.showDelBtn;
		if(showBtn == "T"){
			uploadBtnShow = false;
		}else if(objectId){
			uploadBtnShow = true;
		}else{
			uploadBtnShow = false;
		}
		if(showDelBtn == "T"){
			showDelBtn = false;
		}else if(objectId){
			showDelBtn = true;
		}else{
			showDelBtn = false;
		}
		
		//如果非图片,使用统一的文件图标代替
		var fileJpg = contextPath + "/static/images/file.jpg";
		//上传图片展示框
		var showImgPanel = Ext.create('Ext.Panel', {
			id:"show_img_panel",
			height:180,
	        frame: true,
	        collapsible: false,
	      	autoScroll:true,
	      	bodyStyle: 'overflow-y:hidden; overflow-x:auto',
	        layout: "hbox"
	    });
		
		
		//附件上传表单
		var ajaxFileUploadForm = Ext.create("Ext.form.Panel", {
			id:"ajaxFileUploadForm",
			border: false,
			items: [{
	            xtype: 'filefield',
	            fieldLabel: "上传",
	            buttonText: "上传",
	            margin: "10 0 0 0",
	            name: "file",
	            layout: "hbox",
	            hidden: uploadBtnShow,
	            validator: function(value){
	            	//调用此附件组件,不限制文件类型
	            	//2016-12-28_JHSong
	            	if(value){
	            		//6为流程图片,可上传任意文件
	            		if("6" == imageType){
	            			return true;
	            		}else if(!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG|BMP)$/.test(value)){
				      		return true;
					    }else{
					    	return true;
					    }
            		}else{
				    	return true;
				    }
				},
	            listeners: {
	        		change: function(file){
	        			var form = ajaxFileUploadForm.getForm();
		                if(form.isValid()){
			                form.submit({
			                    url: contextPath + '/common/file/extUploadFile?type=1',
			                    waitMsg: '附件上传中,请稍候...',
			                    success: function(form, action) {
			                    	var fileName = action.result.fileName;
			                    	var httpUrl = action.result.url;
			                    	//是否为文件
			                		var fileFlag = true;
			                    	if((/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG|BMP)$/.test(fileName))){
			    	            		fileFlag = false;
			    	            	}
			                    	var imgs="<a href='"+httpUrl+"' target='_blank'><img style='width: 80px; height: 90px;' title='"+fileName+"' src='"+httpUrl+"'/></a>";
			                    	if(fileFlag){
			                    		imgs="<a href='"+httpUrl+"' target='_blank'><img style='width: 80px; height: 90px;' title='"+fileName+"' src='"+fileJpg+"'/></a>";
			                    	}
			                    	var imgPanel = Ext.create("Ext.Panel", {
			                    		title:fileName,
								        html: imgs,
								        buttons: [
								           {text:'删除',handler:function(){
								        	   var fileArr = ajaxFileUploadForm.getForm().findField("fileArr").getValue();
								        	   if(null==fileArr || fileArr=="")return;
								        	   var fileArrs=fileArr.split("|");
								        	   var newFileArr="";
								        	   for(var i=0;i<fileArrs.length;i++){
								        		   if(fileArrs[i]!=httpUrl){
								        			   if(!newFileArr){
								        				   newFileArr = fileArrs[i];
														}else{
															newFileArr = newFileArr + "|" + fileArrs[i];
														}
								        		   }
								        	   }
								        	   ajaxFileUploadForm.getForm().findField("fileArr").setValue(newFileArr);
								        	   
								        	   var fileArrName = ajaxFileUploadForm.getForm().findField("fileArrNames").getValue();
								        	   var fileArrNames=fileArrName.split("|");
								        	   var newFileArrNames="";
								        	   for(var i=0;i<fileArrNames.length;i++){
								        		   if(fileArrNames[i]!=fileName){
								        			   if(!newFileArrNames){
								        				   newFileArrNames = fileArrNames[i];
														}else{
															newFileArrNames = newFileArrNames + "|" + fileArrNames[i];
														}
								        		   }
								        	   }
								        	   ajaxFileUploadForm.getForm().findField("fileArrNames").setValue(newFileArrNames);
								        	   imgPanel.close();
								           }}
								        ]
			                    	})
									showImgPanel.add(imgPanel);
									//添加隐藏字符串
									var fileArr = ajaxFileUploadForm.getForm().findField("fileArr").getValue();
									if(!fileArr){
										fileArr = httpUrl;
									}else{
										fileArr = fileArr + "|" + httpUrl;
									}
									ajaxFileUploadForm.getForm().findField("fileArr").setValue(fileArr);
									//文件名称
									var fileArrNames = ajaxFileUploadForm.getForm().findField("fileArrNames").getValue();
									if(!fileArrNames){
										fileArrNames = fileName;
									}else{
										fileArrNames = fileArrNames + "|" + fileName;
									}
									ajaxFileUploadForm.getForm().findField("fileArrNames").setValue(fileArrNames);
									Ext.getCmp("certification").show();
				       			},
				       			failure: function(form, action){
		                        	Ext.Msg.alert('Failure', 'Your Attachement "' + action.result.fileName + '" has been uploaded.');
				       			}
			                });
		                }
	        		},
	        		afterRender: function(){
	        			if(objectId){
	        				var params = {};
	        				params["objectId"] = objectId;
	        				params["imageType"] = imageType;
	        				Ext.Ajax.request({
								method: "POST",
								url: contextPath + "/backstage/workflow/process/queryProcessImages",
								params: params,
								success: function(response, options){
									var jsonResult = "";
									if(response.responseText){
										jsonResult = Ext.decode(response.responseText);
									}
									var returnRecord = jsonResult.length;
									if(returnRecord > 0){
										for(var ii=0; ii<jsonResult.length; ii++){
											var record = jsonResult[ii];
											var httpUrl = record.smallUrl;
											var fileName = httpUrl.substring(httpUrl.lastIndexOf("/")+1,httpUrl.length);
											var fileShowName=record.title;
					                    	//是否为文件
					                		var fileFlag = true;
					                    	if((/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG|BMP)$/.test(fileName))){
					    	            		fileFlag = false;
					    	            	}
					                    	
					                    	var imgs="<a href='"+httpUrl+"' target='_blank'><img style='width: 80px; height: 80px;' title='"+fileName+"' src='"+httpUrl+"'/></a>";
					                    	if(fileFlag){
					                    		imgs="<a href='"+httpUrl+"' target='_blank'><img style='width: 80px; height: 80px;' title='"+fileName+"' src='"+fileJpg+"'/></a>";
					                    	}
					                    	
					                    	var imgPanel = createPanel(httpUrl, fileName, fileShowName, imgs, ajaxFileUploadForm, showDelBtn);
											showImgPanel.add(imgPanel);
					                    	//添加隐藏字符串
											var fileArr = ajaxFileUploadForm.getForm().findField("fileArr").getValue();
											if(!fileArr){
												fileArr = httpUrl;
											}else{
												fileArr = fileArr + "|" + httpUrl;
											}
											ajaxFileUploadForm.getForm().findField("fileArr").setValue(fileArr);
											//文件名称
											var fileArrNames = ajaxFileUploadForm.getForm().findField("fileArrNames").getValue();
											if(!fileArrNames){
												fileArrNames = fileShowName;
											}else{
												fileArrNames = fileArrNames + "|" + fileShowName;
											}
											ajaxFileUploadForm.getForm().findField("fileArrNames").setValue(fileArrNames);
											Ext.getCmp("certification").show();
										}
									}
								},
								failure: function(response, options){
									Ext.Msg.alert('错误', '请求超时.');
								}
							});
	        			}
	        		}
	        	}
	        },{
	        	xtype: "fieldcontainer",
	        	fieldLabel: "附件",
	        	margin: "10 0 0 0",
	        	id: "certification",
	        	name: "certification",
	        	border:true,
	        	width:'80%',
	        	items: [showImgPanel]
	        },{
            	xtype : 'textfield', fieldLabel : '收款凭证', id: "fileUrlArr",
		    	name: "fileArr", readOnly: true, hidden: true
            },{
            	xtype : 'textfield', id: "fileArrNames",
		    	name: "fileArrNames", readOnly: true, hidden: true
            },{
            	xtype : 'textfield', fieldLabel : '图片类型', id: "imageType",
		    	name: "imageType", readOnly: true, hidden: true, value: imageType
            }]
		});
		
		var defaultConfig = {
			margin: "10 0 0 0",
			title:"附件",
			height:260,
			items:[ajaxFileUploadForm]
		};
		
		Ext.apply(this,defaultConfig);
		this.callParent();
	}
});

function createPanel(httpUrl, fileName, fileShowName, imgs, ajaxFileUploadForm, showDelBtn){
	var panel = Ext.create("Ext.Panel", {
		title:fileShowName,
        html: imgs,
        buttons: [
           {text:'删除', hidden: showDelBtn, handler:function(){
        	   var fileArr = ajaxFileUploadForm.getForm().findField("fileArr").getValue();
        	   if(null==fileArr || fileArr=="")return;
        	   var fileArrs=fileArr.split("|");
        	   var newFileArr="";
        	   for(var i=0;i<fileArrs.length;i++){
        		   if(fileArrs[i]!=httpUrl){
        			   if(!newFileArr){
        				   newFileArr = fileArrs[i];
						}else{
							newFileArr = newFileArr + "|" + fileArrs[i];
						}
        		   }
        	   }
        	   ajaxFileUploadForm.getForm().findField("fileArr").setValue(newFileArr);
        	   
        	   var fileArrName = ajaxFileUploadForm.getForm().findField("fileArrNames").getValue();
        	   var fileArrNames=fileArrName.split("|");
        	   var newFileArrNames="";
        	   for(var i=0;i<fileArrNames.length;i++){
        		   if(fileArrNames[i]!=fileName){
        			   if(!newFileArrNames){
        				   newFileArrNames = fileArrNames[i];
						}else{
							newFileArrNames = newFileArrNames + "|" + fileArrNames[i];
						}
        		   }
        	   }
        	   ajaxFileUploadForm.getForm().findField("fileArrNames").setValue(newFileArrNames);
        	   panel.close();
           }}
        ]
	})
	return panel
}